package com.gofunbuy.app.book;
import com.gofunbuy.R;
import com.gofunbuy.TabStoreGroup;
import com.gofunbuy.util.GoFunBuyUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class TabBookActivity extends Activity {
	//http://www.eye4gift.com/gofun/app_orderseat.php?user_id=0
	private WebView mWebView;
	private String userId;
	private ProgressBar mProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.book_layout);		
		userId = GoFunBuyUtils.mUserID==null?"0":GoFunBuyUtils.mUserID;		
		
		init(); 
	      
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(!GoFunBuyUtils.isNetworkConnected(TabBookActivity.this)){
			Toast.makeText(TabBookActivity.this, "网络状况不好！", 2000).show();
			return;
		}
		webHtml();
		
	}
	private void webHtml() {  
		 new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {  	 
					mWebView.loadUrl("http://www.eye4gift.com/gofun/app_orderseat.php?shopcode=goinside&user_id="+userId);
				 
				} catch (Exception ex) {  
				 
					ex.printStackTrace();  
				 
				}  
			}
			 
		 }).start();
		
	}
	private void init(){
		
		TextView tvTitle = (TextView) findViewById(R.id.store_topbar_title);
		tvTitle.setText(R.string.title_book);
		
		TextView back = (TextView)findViewById(R.id.store_topbar_back);
		back.setVisibility(View.GONE);	
		TextView buyList = (TextView) findViewById(R.id.store_topbar_buylist);
		buyList.setVisibility(View.GONE);	   
		
		mWebView = (WebView) findViewById(R.id.book_web);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebChromeClient(new MyWebChromeClient());
		
		mWebView.setWebViewClient(new WebViewClient(){       
            public boolean shouldOverrideUrlLoading(WebView view, String url) {       
                view.loadUrl(url);       
                return true;       
            }  
            public void onPageFinished(WebView view, String url){ 
            	mProgressBar.setVisibility(View.GONE);
            	mWebView.setVisibility(View.VISIBLE);
            } 
            public void onPageStarted(WebView view, String url, Bitmap favicon) { 
            	mProgressBar.setVisibility(View.VISIBLE);
            	mWebView.setVisibility(View.GONE);
            } 
            
		});
		
		mProgressBar = (ProgressBar)findViewById(R.id.book_progress);
	}	
	private class MyWebChromeClient extends WebChromeClient {
	 
		@Override
		public boolean onJsAlert(WebView view, String url, String message,
				final JsResult result) {
			new AlertDialog.Builder(TabBookActivity.this)
					.setTitle("")
					.setMessage(message)
					.setPositiveButton(android.R.string.ok,
							new AlertDialog.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// do your stuff
									result.confirm();
								}
							}).setCancelable(false).create().show();
			return true;
		}

		@Override
		public boolean onJsConfirm(WebView view, String url, String message,
				final JsResult result) {
			new AlertDialog.Builder(TabBookActivity.this)
					.setTitle("")
					.setMessage(message)
					.setPositiveButton(android.R.string.ok,
							new AlertDialog.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// do your stuff
									result.confirm();
								}
							}).setCancelable(false).create().show();
			return true;
		}

		@Override
		public boolean onJsPrompt(WebView view, String url, String message,
				String defaultValue, final JsPromptResult result) {
			new AlertDialog.Builder(TabBookActivity.this)
					.setTitle("")
					.setMessage(message)
					.setPositiveButton(android.R.string.ok,
							new AlertDialog.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// do your stuff
									result.confirm();
								}
							}).setCancelable(false).create().show();
			return true;
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private boolean isExit = false;

	public void exit() {
		Log.v("tabBook", "exit");
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(MSG_BACK_CLICKED, 2000);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}
	private static final int MSG_BACK_CLICKED = 1;
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_BACK_CLICKED:
				isExit = false;
				break;
			}
		}
	};

}